package com.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.model.User;
import com.service.ArticleService;
import com.service.QuestionService;
import com.service.UserService;

@Controller
@RequestMapping("/")
public class IndexController {
	
	@Autowired
	private UserService userService;

	@Autowired
	private QuestionService questionService;

	@Autowired
	private ArticleService articleService;
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView index(ModelAndView mav, HttpSession session) {
		List hottags = userService.findHotTag(30);
		List activeUsers = userService.findActiveUserByPage(1, 6).getList();
		List hotUsers = userService.findHotUserByPage(1, 6).getList();
		List newArticle = articleService.findNewArticleTopN(10);
		List hotArticle = articleService.findHotArticleTopN(10);
		
		List newQuestion = questionService.findNewQuestionTopN(7);
		List hotQuestion = questionService.findHotQuestionTopN(6);
		
		mav.addObject("hottags",hottags);
		mav.addObject("activeUsers",activeUsers);
		mav.addObject("hotUsers",hotUsers);
		mav.addObject("newArticle",newArticle);
		mav.addObject("hotArticle",hotArticle);
		mav.addObject("newQuestion",newQuestion);
		mav.addObject("hotQuestion",hotQuestion);
		mav.setViewName("index");
		return mav;
	}
	@RequestMapping(value = "/{uri}", method = RequestMethod.GET)
	public ModelAndView uHome(ModelAndView mav, HttpSession session,@PathVariable("uri") String uri) {
		User user = userService.findByPriUrl(uri);
		if(user!=null){
			mav.setViewName("forward:c/home/"+user.getId()+".html");
		}else{
			mav.setViewName("404");
		}
		return mav;
	}
}
